import React, { useMemo } from "react";
import EditPage, { createFields } from "../../Component/ResourceList/Edit";
import { LibAppView, utils } from '@/lib';
import { FormattedMessage } from "react-intl";
import { ActivityMode, ActivityStatus } from "./Data";
import dayjs from 'dayjs';
import { SelectArticleFormItem, SelectExpertFormItem } from "./SourceData";


const ActivityEdit = LibAppView.withService({
})((props) => {
    const requireRules = useMemo(() => ([{ required: true, message: <FormattedMessage id="admin.pages.article.formItem.required"/>}]), []);

    const fields = useMemo(() =>createFields([{
        label: "活动标题",
        name: "title",
        type: "Input",
        required: true,
        rules: requireRules
    },{
        label: "开始时间",
        name: "startTime",
        type: "Date",
        required: true,
        rules: requireRules
    }, {
        label: "结束时间",
        name: "endTime",
        type: "Date",
        required: true,
        rules: requireRules
    },{
        label: "主办单位",
        name: "hostOrganization",
        type: "Input",
        required: true,
        rules: requireRules
    },{
        label: "活动方式",
        name: "mode",
        type: "Select",
        required: true,
        rules: requireRules,
        props: {
            options: ActivityMode
        }
    }, {
        label: "活动地区",
        name: "region",
        type: "Region",
        required: true,
        rules: requireRules
    },{
        label: "详细地址",
        name: "address",
        type: "Input"
    }, {
        label: "封面",
        name: "cover",
        type: "Cover",
        props: {
            filterType: "image"
        }
    }, {
        label: "内容",
        name: "content",
        type: "RichText"
    }, {
        label: "是否外链",
        name: "isRef",
        type: "Switch",
        props: {
            checkedChildren: "是",
            unCheckedChildren: "否"
        }
    }, {
        label: "外链地址",
        name: "refUrl",
        type: "Input"
    },
    {
        label: "是否科普月专栏资源",
        name: "popularScience",
        type: "Switch",
        props: {
            checkedChildren: "是",
            unCheckedChildren: "否"
        }
    },
    {
        label: "关联资源",
        name: "articles",
        type: "Custom",
        fullColumn: true,
        Component: SelectArticleFormItem
    }, {
        label: "关联专家",
        name: "resources",
        type: "Custom",
        fullColumn: true,
        Component: SelectExpertFormItem
    }]),[]);
    return (
        <EditPage
            submitEndPoint="activity.submit"
            fields={fields}
            initValues={{
                dataType: "specialType",
                isRef: 0,
                status: 0
            }}
            buildRequest={(formData: any) => {
                return {
                    ...formData,
                    isRef: formData.isRef ? 1 : 0,
                    region: utils.jsonToStr(formData.region),
                    status: /^[\d]+$/.test(formData.status) ? formData.status : 0,
                    popularScience: formData.popularScience ? 1 : 0,
                };
            }}
            buildResponse={(response: any) => {
                return {
                    ...response,
                    isRef: response.isRef === 1,
                    popularScience: response.popularScience === 1,
                    startTime: response.startTime && dayjs(response.startTime),
                    endTime: response.endTime && dayjs(response.endTime)
                }
            }}
        />
    )
});

export default ActivityEdit;

